package websocketgulu.demo.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import websocketgulu.demo.Utils.SendMp3Utils;
import websocketgulu.demo.Utils.TestPathUtils;
import websocketgulu.demo.Utils.TimeConverter;
import websocketgulu.demo.common.lang.Result;
import websocketgulu.demo.pojo.Mp3Details;
import websocketgulu.demo.service.Mp3DataService;

@RestController
@RequestMapping({"/api/audio"})
public class GuluSendMp3Controller {
    @Autowired
    private Mp3DataService mp3DataService;

    @Autowired
    private SendMp3Utils sendMp3Utils;

    @GetMapping({"/details"})
    public Result loadDetailsByUserIdAndDay(@RequestParam("start_time") String start_time, @RequestParam("end_time") String end_time, @RequestParam("userId") Long userId, HttpServletResponse response) {
        start_time = TimeConverter.timeConverter(start_time);
        end_time = TimeConverter.timeConverter(end_time);
        System.out.println(userId + " " + start_time);
        Mp3Details mp3 = this.mp3DataService.loadMp3Details(userId, start_time, end_time);
        String path = mp3.getPath();
        String this_start = mp3.getStart_time();
        String this_end = mp3.getEnd_time();
        SetResponse(path, response);
        this.mp3DataService.send_playback(mp3, start_time, end_time);
        return Result.success("{start_time:" + this_start.toString() + ",end_time:" + this_end
                .toString() + "}");
    }

    @GetMapping("/loadmusic")
    public Result loadMusic(@RequestParam("target") String target, HttpServletResponse response){
        SetResponse(target, response);
        return Result.success("load success!");
    }


    /**
     *
     * @param path 音频的地址
     * @param response http response
     */
    public static void SetResponse(String path, HttpServletResponse response) {
        ServletOutputStream servletOutputStream = null;
        int index = path.lastIndexOf(TestPathUtils.getFlag() );
        String fileName = path.substring(index);
        response.setHeader("content-type", "audio/mpeg");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream outputStream = null;
        try {
            servletOutputStream = response.getOutputStream();
            File file = new File(path);
            bis = new BufferedInputStream(new FileInputStream(file));
            int read = bis.read(buff);
            while (read != -1) {
                servletOutputStream.write(buff, 0, buff.length);
                servletOutputStream.flush();
                read = bis.read(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null)
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (servletOutputStream != null)
                try {
                    servletOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}
